import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';
import data_storage from '@ohos.data.storage';
export default {
    data: {
        title: 'World',
        listData: [{"iconSrc":"common/icon/ic_collection.png","iconName":"收藏"},
                   {"iconSrc":"common/Discover/friends-circle.png","iconName":"朋友圈"},
                   {"iconSrc":"common/icon/card_bag.png","iconName":"卡包"},
                   {"iconSrc":"common/icon/ic_expression.png","iconName":"表情"},
                   ],
        imgSrc:"common/images/head1.png",
        userName:"",
        weChatNum:0,
        user:{id:0}
    },

    onInit()
    {
        let path = '/data/data/com.example.harmonyos_wechat/files'
        let storage = data_storage.getStorageSync(path + '/mystore')
        storage.get('userName', 'default').then((value) => {
            console.info("The value of userName is " + value)
            this.userName = value;
        }).catch((err) => {
            console.info("Get the value of startup failed with err: " + err)
        })
        storage.get('weChatNum', 1).then((value) => {
            console.info("The value of weChatNum is " + value)
            this.weChatNum = value;
            this.user.id = value
        }).catch((err) => {
            console.info("Get the value of startup failed with err: " + err)
        })
    },

    change(){
        // 每一个httpRequest对应一个http请求任务，不可复用
        let httpRequest = http.createHttp();
        // 用于订阅http响应头，此接口会比request请求先返回。可以根据业务需要订阅此消息

        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
            "http://hjj.vaiwan.com/quit",
            {
                method: 'POST', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json'
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: this.user,
                readTimeout: 60000, // 可选，默认为60000ms
                connectTimeout: 60000 // 可选，默认为60000ms
            },
            (err, data) => {
                if (!err) {
                    // data.result为http响应内容，可根据业务需要进行解析
                    console.info('Result:' + data.result);
                    let result = JSON.parse(data.result);
                    if(result.code == 20000)
                    {
                        router.replace({
                            uri:"pages/login/login"
                        })
                    }
                    else
                    {
                        prompt.showToast({message:"用户名或密码错误"})
                    }
                    console.info('code:' + data.responseCode);
                    // data.header为http响应头，可根据业务需要进行解析
                    console.info('header:' + data.header);
                } else {
                    console.info('error:' + err.data);
                }
            }
        );
    }
}
